package com.sunday.volunteer.admin.fragment.index_tab;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.Constants;
import com.sunday.common.utils.ToastUtils;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.adapter.index.Tab1RecycleAdapter;
import com.sunday.member.base.BaseFragment;
import com.sunday.volunteer.admin.entity.Member;
import com.sunday.volunteer.admin.entity.MyActivities;
import com.sunday.volunteer.admin.event.CancelActivityEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;
import com.sunday.volunteer.admin.ui.index.IndexTab1DetialActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * 进行中
 */
public class IndexTab1Fragment extends BaseFragment {

    @Bind(R.id.recycler_view)
    RecyclerView recyclerView;
    @Bind(R.id.refresh_layout)
    SwipeRefreshLayout refreshLayout;

    private Member member;

    private LinearLayoutManager manager;
    private Tab1RecycleAdapter adapter;
    private List<MyActivities> activitiesList = new ArrayList<>();

    public IndexTab1Fragment() {
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_index_tab1, null);
        ButterKnife.bind(this, view);
        member = BaseApp.getInstance().getMember();
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        manager = new LinearLayoutManager(mContext);
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(manager);
        adapter = new Tab1RecycleAdapter(mContext,activitiesList);
        recyclerView.setAdapter(adapter);
        refreshLayout.setColorSchemeResources(R.color.material_red, R.color.material_green,
                R.color.material_blue, R.color.material_yellow);
        adapter.setItemClickListener(new Tab1RecycleAdapter.ItemClickListener() {
            @Override
            public void itemClick(View v, long id) {
                Intent intent = new Intent(mContext, IndexTab1DetialActivity.class);
                intent.putExtra("id",id);
                startActivity(intent);
            }
        });
        getData();
        handleMore();
    }

    //获取进行中的活动
    private void getData(){
        showLoadingDialog(0);
        Call<ResultDO<List<MyActivities>>> call = VolunteerClient.getVolunteerAdapter().getMyActivitys(member.getId(),999,pageNo,pageSize);
        call.enqueue(new Callback<ResultDO<List<MyActivities>>>() {
            @Override
            public void onResponse(Call<ResultDO<List<MyActivities>>> call, Response<ResultDO<List<MyActivities>>> response) {
                dissMissDialog();
                refreshLayout.setRefreshing(false);
                if (response.body() == null||isFinish){
                    return;
                }
                if (response.body().getCode() == 0) {
                    if (pageNo==1){
                        activitiesList.clear();
                    }
                    activitiesList.addAll(response.body().getResult());
                    adapter.notifyDataSetChanged();
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<List<MyActivities>>> call, Throwable t) {
                dissMissDialog();
                refreshLayout.setRefreshing(false);
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private int pageNo = 1;
    private int pageSize = 20;
    private int lastVisibleItem;
    private boolean isCanloadMore = false;
    private void handleMore(){
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                pageNo = 1;
                getData();
            }
        });
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItem = manager.findLastVisibleItemPosition();
                isCanloadMore = dy>0;
            }

            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE
                        && lastVisibleItem + 1 == adapter.getItemCount() && isCanloadMore) {
                    pageNo++;
                    getData();
                }
            }
        });
    }

    public void onEvent(CancelActivityEvent event){
        getData();
    }
}
